<?php
/**
 * Model class link to table master_tags in database
 *
 * @author Dat Nguyen <datn@evolable.asia>
 */
class Tag extends AppModel {
	var $name = 'Tag';
	var $useTable = 'tags';
	var $tablePrefix = 'master_';
	var $primaryKey = 'id';
	var $order = 'id desc';

	/**
	 * Get list tag of course
	 *
	 * @param $id
	 * @return array
	 */
	function getTagByCourse($id) {
		$options['joins'] = array(
			array(
				'table' => 'tagmap',
				'alias' => 'Tagmap',
				'type' => 'INNER',
				'conditions' => array(
					'Tag.id = Tagmap.tag_id'
				)
			)
		);
		$options['fields'] = array (
			'Tag.*',
		);
		$options['conditions'] = array(
			'Tagmap.course_id' => $id,
		);
		$options['recursive'] = -1;
		return $this->find('all', $options);
	}

	function getTagsForFilter() {
		$options = array(
			'conditions' => array(
				$this->alias . '.id IN (SELECT tag_id FROM tagmap tm INNER JOIN courses c ON tm.course_id = c.id AND c.del_flg = 0 AND c.display_flg = 1)'
			),
			'fields' => array(
				'id', 'name'
			),
			'limit' => 10,
		);

		return $this->find('list', $options);
	}
}